module ShowAndHideHelper

	def	show_simple(link_text, dom_to_show)
		out = "<span id=\"show_#{dom_to_show}\" >"
		out << link_to_function(link_text) do |page|
							page["#{dom_to_show}"].show
						end
		out << "</span>"
		out	
	end
	
	def	show_hide_simple(link_text, dom_to_hide, dom_to_show)
		out = "<span id=\"show_#{dom_to_show}\" >"
		out << link_to_function(link_text) do |page|
							page["#{dom_to_hide}"].hide
							page["#{dom_to_show}"].show
						end
		out << "</span>"
		out	
	end
	
	def	show_hide_single_button(link_text, dom_to_hide, dom_to_show)
		out = ""
		out << "<span id=\"show_#{dom_to_show}\" >"
		out << link_to_function(link_text) do |page|
							page["#{dom_to_hide}"].hide
							page["#{dom_to_show}"].show
							page["show_#{dom_to_show}"].hide
							page["hide_#{dom_to_show}"].show
						end
		out << "</span>"
		out << "<span id=\"hide_#{dom_to_show}\" style=\"display:none\">"
		out << link_to_function(link_text) do |page|
							page["#{dom_to_hide}"].show
							page["#{dom_to_show}"].hide
							page["show_#{dom_to_show}"].show
							page["hide_#{dom_to_show}"].hide
						end
		out << "</span>"
		out	
	end
	
	def	show_hide_toggle_with_class(link_text, class_to_toggle, default = "hide")
		out = ""
		out << "<span id=\"show_#{class_to_toggle}\"" 
		out <<	"	style=\"display:none\"" if default == "hide"
		out << ">"
		out << link_to_remote("show #{link_text}", :url => { :controller => '/account', :action => :save_show_hide, :css_class => class_to_toggle, :setting => 'show' }, 
								:before => render(:update) do |page|
							page.select(".#{class_to_toggle}").each { |el| el.show }
							page["show_#{class_to_toggle}"].hide
							page["hide_#{class_to_toggle}"].show
						end)
		out << "</span>"
		out << "<span id=\"hide_#{class_to_toggle}\" "
		out <<	"	style=\"display:none\"" if default == "show"
		out << ">"
		out << link_to_remote("hide #{link_text}", :url => { :controller => '/account', :action => :save_show_hide, :css_class => class_to_toggle, :setting => 'hide' }, 
								:before => render(:update) do |page|
							page.select(".#{class_to_toggle}").each { |el| el.hide }
							page["show_#{class_to_toggle}"].show
							page["hide_#{class_to_toggle}"].hide
						end)
		out << "</span>"
		out	
	end
	
	def show_hide_buttons(dom_id, show = "show", hide = "hide", starts_as_visible = true)
		out = ""
		out << "<span id=\"show_#{dom_id}\" #{starts_as_visible ? 'style="display:none"' : '' } >"
		out << link_to_function(show) do |page| 	
						page["#{dom_id}"].show
						page["show_#{dom_id}"].hide
						page["hide_#{dom_id}"].show
					end 
		out << "</span>"
		
		out << "<span id=\"hide_#{dom_id}\" #{starts_as_visible ? '' : 'style="display:none"' } >"
		out << link_to_function(hide) do |page| 	
						page["#{dom_id}"].hide
						page["show_#{dom_id}"].show
						page["hide_#{dom_id}"].hide
					end 
		out << "</span>"	
		out
	end

	def switcher_buttons(shown_dom_id, hidden_dom_id, show_hidden = "show hidden", rehide = "rehide")
		out = ""
		out << "<span id=\"show_#{hidden_dom_id}\" >"
		out << link_to_function(show_hidden) do |page| 	
						page["#{hidden_dom_id}"].show
						page["#{shown_dom_id}"].hide
						page["show_#{hidden_dom_id}"].hide
						page["hide_#{hidden_dom_id}"].show
					end 
		out << "</span>"
		
		out << "<span id=\"hide_#{hidden_dom_id}\" style=\"display:none\">"
		out << link_to_function(rehide) do |page| 	
						page["#{hidden_dom_id}"].hide
						page["#{shown_dom_id}"].show
						page["show_#{hidden_dom_id}"].show
						page["hide_#{hidden_dom_id}"].hide
					end 
		out << "</span>"	
		out
	end
	
	
	# give tab_list in the form:
	#		[	{	:name => "tab 1", :target_id => "target_id1" }, 
	#			{ :name => "tab 2", :target_id => "target_id2" }  	]
	def tabs(tab_list, default_tab_name = "", options = {})
		return nil if tab_list.nil? or tab_list.empty? 
		out = "<table class='helper_tabs_wrapper' cellpadding='0' cellspacing='0'><tr>"	
		if tab_list.map {|t| t[:name] }.include?(session[:on_tab])
			default_tab_name = session[:on_tab]
		else
			default_tab_name = default_tab_name.blank? ? tab_list.first[:name] : default_tab_name
		end
		for tab in tab_list
			out << "<td class='helper_tabs_#{tab[:name] == default_tab_name ? 'on' : 'off'}'" 
			out << " id='helper_tab_#{tab[:name].unspace}'"
			out << " style='#{options[:style]}'" if options and options[:style]
			out << ">"
			out << link_to_remote(tab[:name], :url => { :controller => '/account', :action => :save_tab, :tab => tab[:name] }, 
								:before => render(:update) do |page|
									tab_list.each do |t| 
										page[t[:target_id]].hide
										page["helper_tab_#{t[:name].unspace}"].add_class_name("helper_tabs_off").remove_class_name("helper_tabs_on")
									end
									page[tab[:target_id]].show
									page["helper_tab_#{tab[:name].unspace}"].add_class_name("helper_tabs_on").remove_class_name("helper_tabs_off").blur
									
								end)
			out << "</td>"
		end
		out << "</tr></table>"
		out
	end
	
	
end

